package com.atguidu.admin.Controller;

import com.atguidu.admin.bean.Account;
import com.atguidu.admin.bean.City;
import com.atguidu.admin.bean.User;
import com.atguidu.admin.service.AccountService;
import com.atguidu.admin.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.data.redis.core.ValueOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
@Slf4j
@Controller
public class IndexController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    AccountService accountService;

    @Autowired
    CityService cityService;

    //@Autowired
    //StringRedisTemplate redisTemplate;

    /**
     * 登入页
     * @return 返回登入界面
     */
    @GetMapping(value = {"/login", "/"})
    public String loginPage() {
        return "login";
    }

    /**
     * 重定向跳转到主页
     */
    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model) {
        if ("sunshine".equals(user.getUserName()) && "123456".equals(user.getPassword())) {
            //把登入的用户存入session域中
            session.setAttribute("loginUser", user);
            //登入成功重定向到main.html  重定向防止表的重复提交
            return "redirect:/main";
        } else {
            model.addAttribute("msg", "账号密码错误");
            return "login";
        }
    }

    /**
     * 展示主页面
     */
    @GetMapping("/main")
    public String mainPage(HttpSession session, Model model) {
        log.info("当前方法是:{}", "mainPage");
        //设置拦截器便可以不用再这里拦截
        /*//是否登录。  拦截器，过滤器
        Object loginUser = session.getAttribute("loginUser");
        if(loginUser != null){
            return "main";
        }else {
            //回到登录页面
            model.addAttribute("msg","请重新登录");
            return "login";
        }*/

//        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
//        String s = opsForValue.get("/main.html");
//        String s1 = opsForValue.get("/sql");
//        model.addAttribute("mainCount",s);
//        model.addAttribute("sqlCount",s1);
        return "main";
    }

    /**
     * 自定义Druid数据展示
     */
    @ResponseBody
    @GetMapping("sql")
    public String queryFromDb() {
        Long along = jdbcTemplate.queryForObject("select count(*) from account_tbl", Long.class);
        return along.toString();
    }

    /**
     * 配置文件版Mybatis
     */
    @ResponseBody
    @GetMapping("acct")
    public Account getById(@RequestParam("id") Long id) {
        return accountService.getAcctById(id);
    }

    /**
     * 纯注解方式Mybatis
     */
    @ResponseBody
    @GetMapping("city")
    public City getCityById(@RequestParam("id") Long id) {
        return cityService.getById(id);
    }

    /**
     * 混合版Mybatis
     */
    @ResponseBody
    @PostMapping("city")
    public City insertCity(City city) {
        cityService.saveCity(city);
        return city;
    }



}
